<?php
    /*

     Version: MPL 1.1

     Software distributed under the License is distributed on an "AS IS"
     basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
     License for the specific language governing rights and limitations
     under the License.

     The Original Code is KASSIOP Framework.

     The Initial Developer of the Original Code is SURIAN Nicolas (aka NairuS) <me@nairus.fr>.
     Portions created by the Initial Developer are Copyright (C) 2010
     The Initial Developer. All Rights Reserved.

     Contributor(s):

     Alternatively, the contents of this file may be used under the terms
     of the MPL license Version 1.1 (the  "MPL" License"), in which case the
     provisions of Version: MPL 1.1 License are applicable instead of those
     above.  If you wish to allow use of your version of this file only
     under the terms of the MPL License and not to allow others to use
     your version of this file under the MPL, indicate your decision by
     deleting  the provisions above and replace  them with the notice and
     other provisions required by the MPL License.  If you do not delete
     the provisions above, a recipient may use your version of this file
     under either the MPL License.

     The contents of this file are subject to the Mozilla Public License
     Version 1.1 (the "License"); you may not use this file except in
     compliance with the License. You may obtain a copy of the License at
     http://www.mozilla.org/MPL/

    */


    require_once 'PHPUnit/Framework.php';
    require_once dirname( __FILE__ ) . '/../../../kassiop/core/Autoload.php' ;
    Autoload::register() ;

    /**
     * The unit test Configuration class.
     *
     * @package    unittests.core
     * @subpackage kassiop
     * @author NairuS
     *
     */
    class ConfigurationTest extends PHPUnit_Framework_TestCase
    {
        /**
         * The Configuration instance.
         *
         * @var Configuration
         */
        protected $config;

        protected function setUp()
        {
            $this->config = new Configuration( '/demos' ) ;
        }

        protected function tearDown()
        {
            unset( $this->config ) ;
        }

        public function testConstants()
        {
            $this->assertEquals(Configuration::REQUEST_ACTION_PATTERN, '/^\/%s(.*)\/a\/(.*)$/', 'The request action pattern must be well setted.');
            $this->assertEquals(Configuration::REQUEST_PATTERN, '/^\/%s(.*)$/', 'The request pattern must be well setted.');
        }

        public function testIncludeBootstrap()
        {
            $this->assertTrue( Configuration::includeBootstrap(), 'The method must return true.' );
        }

        public function testDSConstant()
        {
            Configuration::includeBootstrap() ;
            $this->assertTrue( defined( 'DS'), 'The DS constant must be defined.' ) ;
            $this->assertEquals( DS, '/', 'The DS constant must be well setted.' ) ;
        }

        public function testAMFPHP_BASEConstant()
        {
            Configuration::includeBootstrap() ;
            $this->assertTrue( defined( 'AMFPHP_BASE'), 'The AMFPHP_BASE constant must be defined.' ) ;
            $this->assertEquals( AMFPHP_BASE, realpath( dirname( __FILE__ )  . '/../../../../www/amfphp/core' ) . '/', 'The AMFPHP_BASE constant must be well setted.' ) ;
        }

        public function testCONFIG_MAIN_PATHConstant()
        {
            Configuration::includeBootstrap() ;
            $this->assertTrue( defined( 'CONFIG_MAIN_PATH'), 'The CONFIG_MAIN_PATH constant must be defined.' ) ;
            $this->assertEquals( CONFIG_MAIN_PATH, realpath( dirname( __FILE__ )  . '/../../../../config' ) . '/', 'The CONFIG_MAIN_PATH constant must be well setted.' ) ;
        }

        public function testConstructor()
        {
            $this->assertEquals($this->config->module, 'demos', '1.1- The module must be "demos".' );
            $this->assertEquals($this->config->action, 'index', '1.2- The action must be "index".' );
            $this->assertEquals($this->config->application, 'frontend', '1.3- The application must be "frontend".' );
            $this->assertEquals($this->config->getRootDirectory(), null, '1.4- The root directory must be empty.' );
            $this->tearDown();

            $this->config = new Configuration( '/administration/demos/a/test', 'backend', 'administration' );
            $this->assertEquals($this->config->module, 'demos', '2.1- The module must be "demos".' );
            $this->assertEquals($this->config->action, 'test', '2.2- The action must be "test".' );
            $this->assertEquals($this->config->application, 'backend', '2.3- The application must be "backend".' );
            $this->assertEquals($this->config->getRootDirectory(), 'administration', '2.4- The root directory must be "administration".' );
            $this->setUp();
            $this->tearDown();

            $this->config = new Configuration( '/' );
            $this->assertEquals($this->config->module, 'demos', '3.1- The module must be "demos".' );
            $this->assertEquals($this->config->action, 'index', '3.2- The action must be "index".' );
            $this->assertEquals($this->config->application, 'frontend', '3.3- The application must be "frontend".' );
            $this->assertEquals($this->config->getRootDirectory(), null, '3.4- The root directory must be empty.' );
            $this->setUp();
        }

        public function testCleanRootDirectory()
        {
            $this->config = new Configuration( '/demos/a/test', 'backend' );
            $this->assertEquals($this->config->cleanRootDirectory('kassiop/www'), "kassiop\/www", '1. The root directory must be cleaned for the pattern.' );

            $this->config = new Configuration( '/demos/a/test', 'backend' );
            $this->assertEquals($this->config->cleanRootDirectory('test/kassiop/www'), "test\/kassiop\/www", '2. The root directory must be cleaned for the pattern.' );
        }

        public function testGetAppsLibs()
        {
            $appsLibs = realpath( dirname( __FILE__ ) . '/../../..' ) . '/applications/' ;
            $this->assertEquals( $this->config->getAppsLibs(), $appsLibs, 'The apps libs must be well setted.' );
        }

        public function testGetDefaultModule()
        {
            $this->assertEquals( $this->config->getDefaultModule(), 'demos', 'The default module must be well setted.' );
        }

        public function testGetPage()
        {
            Configuration::includeBootstrap() ;
            $this->assertTrue( $this->config->getPage() !== false, '1.1- The getPage() method must not return false.' );
            $this->assertEquals( $this->config->getPage(), $this->config->getAppsLibs() . 'frontend/modules/demos/index.php', '1.2- The default current page must be setted.' );
            $this->assertTrue( $this->config->getLayout() !== false, '1.3- The getLayout() method must not return false.' );
            $this->assertEquals( $this->config->getLayout(), $this->config->getAppsLibs() . 'frontend/templates/default.layout.php', '1.4- The default layout must be setted.' );

            $this->config->setLayout( 'test' ) ;
            $this->assertFalse( $this->config->getLayout(), '2.1- The layout "test" must not exist.' ) ;
            $this->tearDown();

            $this->config = new Configuration('/administration/demos_admin', 'backend' ) ;
            $this->assertTrue( $this->config->getPage() !== false, '3.1- The getPage() method must not return false.' );
            $this->assertEquals( $this->config->getPage(), $this->config->getAppsLibs() . 'backend/modules/demos_admin/index.php', '3.2- The default current page must be setted.' );
            $this->assertTrue( $this->config->getLayout() !== false, '3.3- The getLayout() method must not return false.' );
            $this->assertEquals( $this->config->getLayout(), $this->config->getAppsLibs() . 'backend/templates/default.layout.php', '3.4- The default layout must be setted.' );

            $this->tearDown();
            $this->setUp();
        }

        public function testGetPattern()
        {
            $this->assertEquals($this->config->getPattern(), '/^\/(.*)$/', '1.1 - The pattern must be well formatted.');
            $this->tearDown();

            $this->config = new Configuration( '/administration/demos/a/test', 'backend', 'administration' );
            $this->assertEquals($this->config->getPattern(), '/^\/administration\/(.*)$/', '1.2 - The pattern must be well formatted.');

            $this->setUp();
        }

        public function testGetPatternWithAction()
        {
            $this->assertEquals($this->config->getPatternWithAction(), '/^\/(.*)\/a\/(.*)$/', '1.1 - The pattern must be well formatted.');
            $this->tearDown();

            $this->config = new Configuration( '/administration/demos/a/test', 'backend', 'administration' );
            $this->assertEquals($this->config->getPatternWithAction(), '/^\/administration\/(.*)\/a\/(.*)$/', '1.2 - The pattern must be well formatted.');

            $this->setUp();
        }

        public function testModule()
        {
            $this->assertTrue( class_exists( StringsHelper::camelize( $this->config->module . '_controller' ) ), 'The controller must be declared.' );
        }
    }
?>